package com.libs.utils;


import com.libs.sys.model.Userseat;
import com.libs.sys.service.LibService;
import com.libs.sys.service.impl.LibServiceImpl;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.Calendar;

@Slf4j
public class SignThread implements Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see Thread#run()
     */

    private Userseat userseat;
    private final LibService libService = new LibServiceImpl();

    public SignThread(Userseat userseat) {
        this.userseat = userseat;
    }

    @SneakyThrows
    @Override
    public void run() {
        boolean fin = false;
        while (!fin && Calendar.getInstance().get(Calendar.HOUR) < 6) {
            long begin = System.currentTimeMillis();
            fin = libService.signSeat(userseat, HttpClientConn.getHttpClient());
            if (fin) {
                log.info(userseat.getUid() + "签到成功！");
            } else {
                log.info(userseat.getUid() + "签到失败！");
            }
            long finish = System.currentTimeMillis();
            log.info("{}签到耗时{}s", userseat.getUid(), (finish - begin) / 1000);
        }
        if (!fin) {
            log.info("{}没有签到成功！赶快签到！！！", userseat.getUid());
        } else {
            log.info("{}签到成功，安心睡觉啊", userseat.getUid());
        }

    }


}
